package com.younion.util;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;

public class HttpRequestUtils {

	private static Logger logger = Logger.getLogger(HttpRequestUtils.class);

	/**
	 * 把request请求中的参数装入map- 请求中的参数名没有重复的情况
	 * 
	 * @param request
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public static Map<String, String> parameterNoRepeatToMap(
			HttpServletRequest request) {
		Map<String, String> map = new HashMap<String, String>();
		Enumeration<String> enumratrion = request.getParameterNames();
		while (enumratrion.hasMoreElements()) {
			String paramName = (String) enumratrion.nextElement();
			String values = request.getParameter(paramName);
			map.put(paramName, values);
		}
		return map;
	}

	/**
	 * 输出返回的jsonStr
	 * 
	 * @param content
	 * @param response
	 */
	public static void printContent(String content, HttpServletResponse response) {
		response.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=UTF-8");
		PrintWriter writer = null;
		try {
			writer = response.getWriter();
		} catch (IOException e) {
			logger.debug("invalid request", e);
		}
		if (StringUtils.isEmpty(content)) {
			writer.write(JsonUtils.EMPTY_COLLECTION);
		} else
			writer.write(content);
		try {
			response.flushBuffer();
			writer.close();
		} catch (IOException e) {
			logger.error("flush response error", e);
		}
	}

	/**
	 * 对非空参数进行认证 若是空则返回false
	 * 
	 * @param paramName
	 * @param paramValue
	 * @param response
	 * @return
	 */
	public static boolean checkNotEmptyParam(String paramName,
			String paramValue, HttpServletResponse response) {
		boolean isValid = true;
		if (StringUtils.isEmpty(paramValue)) {
			isValid = false;
			String errorReason = paramName + "参数不合法,必须有值!";
			HttpRequestUtils.printContent(errorReason, response);
		}
		return isValid;
	}
	
	/**
	 * 检查参数是否为空
	 * @param paramName
	 * @param paramValue
	 * @param response
	 * @return
	 */
	public static boolean checkIsEmptyParam(String paramName,
			String paramValue, HttpServletResponse response) {
		boolean isValid = true;
		if (StringUtils.isEmpty(paramValue)) {
			isValid = false;
		}
		return isValid;
	}
	
	/**
	 * 对必须为整数的参数进行是否数字的判断
	 * 
	 * @param paramName
	 * @param paramValue
	 * @param response
	 * @return
	 */
	public static boolean checkNeedIntegerParam(String paramName,
			String paramValue, HttpServletResponse response) {
		boolean isValid = false;
		try {
			isValid = Validator.isInteger(paramValue);
		} catch (Exception e) {
			isValid = false;
		}
		if (!isValid) {
			String errorReason = paramName + "值不合法，必须为整数!";
			HttpRequestUtils.printContent(errorReason, response);
		}
		return isValid;
	}
	/**
	 * 检查是否为整数 是返回true 否返回false
	 * @param paramName
	 * @param paramValue
	 * @param response
	 * @return
	 */
	public static boolean checkIntegerParam(String paramName,
			String paramValue, HttpServletResponse response) {
		boolean isValid = false;
		try {
			isValid = Validator.isInteger(paramValue);
		} catch (Exception e) {
			isValid = false;
		}
		return isValid;
	}

	/**
	 * 根据高和宽转换成android对应的分辨率
	 * 
	 * @param wight
	 * @param height
	 * @return
	 */
	private String conversionAndroidResolution(String resolution) {
		String[] primevalResolutionArray = resolution.split("\\_");
		int width = Integer.parseInt(primevalResolutionArray[0]);
		int height = Integer.parseInt(primevalResolutionArray[1]);
		// 默认
		String standardResolution = "320_480";
		if (width <= 320 && height <= 480) {
			standardResolution = "320_480";
		} else if (width < 520 && height <= 854) {
			standardResolution = "480_800";
		} else if (width >= 520 && height > 854) {
			standardResolution = "540_960";
		}
		return standardResolution;
	}
}
